package com.easidea.tim.flex.remote.finance
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.bean.Page;
	import com.easidea.tim.flex.bean.customer.Customer;
	import com.easidea.tim.flex.bean.finance.CustomerAccountListing;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import net.digitalprimates.persistence.hibernate.rpc.HibernateRemoteObject;

	public class CustomerAccountListingDao extends BaseDao
	{
		private var _CustomerAccountListingService:HibernateRemoteObject = new HibernateRemoteObject();
		private static var _instance:CustomerAccountListingDao;
		
		public static function getInstance():CustomerAccountListingDao{
			if( !_instance ){
				_instance = new CustomerAccountListingDao();
				
			}
			return _instance;
		}
		public function CustomerAccountListingDao() {
				_CustomerAccountListingService.channelSet = this.buildChannelSet();
				_CustomerAccountListingService.destination="customerAccountListingService";	
				_CustomerAccountListingService.concurrency = "last";
				_CustomerAccountListingService.makeObjectsBindable = true;	
				_CustomerAccountListingService.showBusyCursor = true;
		}
		
		
		/**
		 * 客户收支财务数据对账
		 * @param 客户ID 
		 * @param 开始日期
		 * @param 结束日期
		 * @param page 
		 * 
		 */
		public function getAccoutListing(customerId:int,startDate:Date, endDate:Date, page:Page):void{
			_CustomerAccountListingService.addEventListener(ResultEvent.RESULT, getAccountListingResult);
			_CustomerAccountListingService.addEventListener(FaultEvent.FAULT,_Fault);
			_CustomerAccountListingService.getAccoutListing(customerId, startDate,endDate, page);
		}
	
//		/**
//		 * 客户收支财务数据对账
//		 * @param 客户名称
//		 * @param 销售人员名称
//		 * @param 开始日期
//		 * @param 结束日期
//		 */
//		public function getAccountListing(customerName:String, salesName:String, 
//			startDate:Date, endDate:Date, page:Page):void{
//			_CustomerAccountListingService.addEventListener(ResultEvent.RESULT, getAccountListingResult);
//			_CustomerAccountListingService.addEventListener(FaultEvent.FAULT,_Fault);
//			_CustomerAccountListingService.getAccoutListing(customerName,salesName, startDate,endDate, page);
//		}
		
		private function getAccountListingResult(e:ResultEvent):void{
			_CustomerAccountListingService.removeEventListener(ResultEvent.RESULT,getAccountListingResult);
			Env.dispatchEvent(new CustomerEvent("getAccountListingSuccess",e.result));
		}
		
		/**
		 * 根据客户信息查看客户收支记录
		 * @param customerName 客户名
		 * @param year 查询年
		 * @param month 查询月
		 * 
		 */
		public function getCustomerAccountListingArray(customerName:String,year:int,month:int, page:Page):void{
			_CustomerAccountListingService.addEventListener(ResultEvent.RESULT, getCustomerAccountListingArrayResult);
			_CustomerAccountListingService.addEventListener(FaultEvent.FAULT,_Fault);
			_CustomerAccountListingService.getCustomerAccountListingArray(customerName,year,month,page);
		}
		
		private function getCustomerAccountListingArrayResult(e:ResultEvent):void{
			_CustomerAccountListingService.removeEventListener(ResultEvent.RESULT,getCustomerAccountListingArrayResult);
			Env.dispatchEvent(new CustomerEvent("getCustomerAccountListingArraySuccess",e.result));
		}
		
		/**
		 *  添加客户收支财务数据
		 *
		 */
		public function addCustomerAccoutListing(acctList:CustomerAccountListing):void{
			_CustomerAccountListingService.addEventListener(ResultEvent.RESULT, addAccoutListingResult);
			_CustomerAccountListingService.addEventListener(FaultEvent.FAULT,_Fault);
			_CustomerAccountListingService.addCustomerAccoutListing(acctList);
		}
		
		private function addAccoutListingResult(e:ResultEvent):void{
			_CustomerAccountListingService.removeEventListener(ResultEvent.RESULT,addAccoutListingResult);
			Env.dispatchEvent(new CustomerEvent("addCustomerAccoutListingSuccess", e.result));
		}
		
		// 获取客户账龄报表
		public function getAccountAgeReport():void{
			_CustomerAccountListingService.addEventListener(ResultEvent.RESULT, getAccountAgeReportResult);
			_CustomerAccountListingService.addEventListener(FaultEvent.FAULT,_Fault);
			_CustomerAccountListingService.accountAgeReport();
		}
		
		private function getAccountAgeReportResult(e:ResultEvent):void{
			_CustomerAccountListingService.removeEventListener(ResultEvent.RESULT,getAccountAgeReportResult);
			Env.dispatchEvent(new CustomerEvent("getAccountAgeReportSuccess",e.result));
		}
	}
}